﻿Imports System.Linq
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.DataAcessLayer.StatementSources
Imports SISSAT.DataAcessLayer.PersistentStorage
Imports SISSAT.BusinessLayer.Utilities

Namespace Services
    Public Class MatriculationService
        Inherits ServiceBase
        Implements IMatriculationService

        Public Sub Edit(employeeId As Integer, scheduleId As Integer, gradePoint As Decimal, modifiedBy As Integer) Implements IMatriculationService.Edit
            StatementSource = New UpdateMatriculationStatementSource(employeeId, scheduleId, gradePoint, modifiedBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Function ReadAllByEmployeeId(employeeId As Integer) As IEnumerable(Of Matriculation) Implements IMatriculationService.ReadAllByEmployeeId
            Dim statement As IStatementSource = New ReadAllMatriculationByEmployeeIdStatementSource(employeeId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(statement)

            Return DataMapper.MapDataToEntity(Of Matriculation)(dataReader)
        End Function

        Public Function ReadAllByScheduleId(scheduleId As Integer) As IEnumerable(Of Matriculation) Implements IMatriculationService.ReadAllByScheduleId
            Dim statement As IStatementSource = New ReadAllMatriculationByScheduleIdStatementSource(scheduleId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(statement)

            Return DataMapper.MapDataToEntity(Of Matriculation)(dataReader)
        End Function

        Public Function ReadById(employeeId As Integer, scheduleId As Integer) As Matriculation Implements IMatriculationService.ReadById
            Dim statement As IStatementSource = New ReadMatriculationStatementSource(employeeId, scheduleId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(statement)

            Dim entities As IEnumerable(Of Matriculation) = DataMapper.MapDataToEntity(Of Matriculation)(dataReader)
            Return entities.FirstOrDefault()
        End Function

        Public Function ReadGradePoint(employeeId As Integer, scheduleId As Integer) As Decimal Implements IMatriculationService.ReadGradePoint
            Dim statement As IStatementSource = New ReadMatriculationStatementSource(employeeId, scheduleId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(statement)

            Dim entities As IEnumerable(Of Matriculation) = DataMapper.MapDataToEntity(Of Matriculation)(dataReader)
            Dim entity As Matriculation = entities.FirstOrDefault()

            Return entity.GradePoint
        End Function

    End Class
End Namespace

